package com.wanxi.servlet.company;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.CompanyModel;
import com.wanxi.service.CompanyService;
import com.wanxi.service.Impl.CompanyServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Li
 * @Date 2023/5/5 16:27
 */
@WebServlet("/back/CompanyInfoAdd")
public class CompanyInfoAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2.接收前端数据
        String logo = req.getParameter("logo");
        String name = req.getParameter("name");
        String landLine = req.getParameter("landLine");
        String mobilePhone = req.getParameter("mobilePhone");
        String email = req.getParameter("email");
        String fax = req.getParameter("fax");
        String zipCode = req.getParameter("zipCode");
        String address = req.getParameter("address");
        String enable = req.getParameter("enable");
        String remark = req.getParameter("remark");
        //封装model
        CompanyModel companyModel = CompanyModel.builder()
                .logo(Tool.nullToStr(logo))
                .name(Tool.nullToStr(name))
                .landLine(Tool.nullToStr(landLine))
                .email(Tool.nullToStr(email))
                .fax(Tool.nullToStr(fax))
                .mobilePhone(Tool.nullToStr(mobilePhone))
                .zipCode(Tool.nullToStr(zipCode))
                .address(Tool.nullToStr(address))
                .enable(Tool.nullToInt(enable))
                .remark(Tool.nullToStr(remark))
                .build();
        //调用service层
        CompanyService companyService = new CompanyServiceImpl();
        ResultDto resultDto = companyService.add(companyModel);
        //返回给前端数据
        resp.getWriter().println(JSONObject.toJSONString(resultDto));
    }
}
